﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;
using MyException;

namespace Maintain_supplier_information
{
    public partial class ViewStockCard : System.Web.UI.Page
    {
        SupplierBizlogic sbz = new SupplierBizlogic();
        DateTime from_date;
        DateTime to_date;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId < 5 || roleId > 7)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                if (!IsPostBack)
                {
                    Calendar1.Visible = false;
                    Calendar2.Visible = false;
                    List<object> categories = new List<object>();
                    categories = sbz.getcategories();

                    ddl_category.DataSource = categories;
                    ddl_category.DataTextField = "CategoryName";
                    ddl_category.DataValueField = "CategoryID";
                    ddl_category.DataBind();

                    int sval = Convert.ToInt32(ddl_category.SelectedValue.ToString());
                    List<object> items = new List<object>();
                    items = sbz.getCatItems(sval);

                    ddl_itemName.DataSource = items;
                    ddl_itemName.DataTextField = "ItemName";
                    ddl_itemName.DataValueField = "ItemCode";
                    ddl_itemName.DataBind();

                    HideFields();
                }

                lblError.Text = "";
            }
            catch (Exception ex)
            {
            }
        }       

        protected void imgbtn_frmdate_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                from_date = Calendar1.SelectedDate;
                Calendar1.Visible = true;
            }
            catch (Exception ex)
            {
            }
        }

        protected void imgbtn_todate_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                Calendar2.Visible = true;
            }
            catch (Exception ex)
            {
            }
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                txt_fromDate.Text = Calendar1.SelectedDate.ToShortDateString();
                from_date = Calendar1.SelectedDate;
                Calendar1.SelectedDates.Clear();
                Calendar1.Visible = false;
            }
            catch (Exception ex)
            {
            }
        }

        protected void Calendar2_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                from_date = Calendar1.SelectedDate;
                if (Calendar2.SelectedDate >= from_date)
                {
                    txt_toDate.Text = Calendar2.SelectedDate.ToShortDateString();
                    Calendar2.SelectedDates.Clear();
                    Calendar2.Visible = false;
                }
                else
                {
                    txt_toDate.Text = "Date error";
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void ddl_category_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int sval = Convert.ToInt32(ddl_category.SelectedValue.ToString());
                List<object> catitems = new List<object>();
                catitems = sbz.getCatItems(sval);
                ddl_itemName.DataSource = catitems;
                ddl_itemName.DataTextField = "ItemName";
                ddl_itemName.DataValueField = "ItemCode";
                ddl_itemName.DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        protected void btn_View_Click(object sender, EventArgs e)
        {
            try
            {
                Calendar1.Visible = false;
                Calendar2.Visible = false;
                String itemCode = ddl_itemName.SelectedValue.ToString();

                Stationery sat = sbz.getStationery(itemCode);

                lbl_ic.Text = sat.ItemCode;
                lbl_itemdesc.Text = sat.ItemName;
                lbl_uom_val.Text = sat.UOM;

                List<SupplierDetail> supplierList = sbz.getSupplierDetailByItemCode(itemCode);

                if (supplierList.Count > 0)
                {
                    lbl_fsup.Text = supplierList[0].Supplier.SupplierName;
                    lbl_secsup.Text = supplierList[1].Supplier.SupplierName;
                    lbl_thirdsup.Text = supplierList[2].Supplier.SupplierName;
                    string itemCode1 = ddl_itemName.SelectedValue.ToString();
                    List<object> list = new List<object>();
                    List<StockHistory> ll = new List<StockHistory>();
                    ll = sbz.getStockHistoryByItemCode(itemCode);

                    DateTime startDate = Convert.ToDateTime(txt_fromDate.Text);
                    DateTime endDate = Convert.ToDateTime(txt_toDate.Text);
                    if (endDate < startDate)
                    {
                        throw new EndDateBeforeStartDateException();
                    }
                    ll = sbz.getStockHistoryByDate(ll, startDate, endDate);
                    Gdview_Stockcard.DataSource = ll;
                    Gdview_Stockcard.DataBind();
                    if (ll.Count <= 0)
                    {
                        lblMessage.Text = "No Stock Card for this item is found for the selected period";
                    }
                    ShowFields();
                }
                else
                {
                    ClearFields();
                    lblMessage.Text = "No Stock Card for this item is found";
                    Gdview_Stockcard.DataSource = null;
                    Gdview_Stockcard.DataBind();
                }
            }
            catch (EndDateBeforeStartDateException ex)
            {
                lblError.Text = "To Date cannot be before From Date";
            }
            catch (Exception ex)
            {
            }
        }

        private void HideFields()
        {
            lblMessage.Text = "";
            lbl_1stSupplier.Visible = false;
            lbl_2ndsupplier.Visible = false;
            lbl_3rdSupplier.Visible = false;
            lbl_itemCode.Visible = false;
            lbl_itemDescription.Visible = false;
            lbl_UOM.Visible = false;
        }

        private void ShowFields()
        {
            lbl_1stSupplier.Visible = true;
            lbl_2ndsupplier.Visible = true;
            lbl_3rdSupplier.Visible = true;
            lbl_itemCode.Visible = true;
            lbl_itemDescription.Visible = true;
            lbl_UOM.Visible = true;
        
        }

        public void ClearFields()
        {
            HideFields();
            lbl_fsup.Text = "";
            lbl_secsup.Text = "";
            lbl_thirdsup.Text = "";
            lbl_ic.Text = "";
            lbl_itemdesc.Text = "";
            lbl_uom_val.Text = "";
            lblMessage.Text = "";
        }
    }
}